package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealPageVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;


    /**
     * 记录有多少条数据
     */
    Integer total;

    Integer PAGESIZE = 10;

    /**
     * 套餐管理分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult setmealPage(SetmealPageQueryDTO setmealPageQueryDTO) {

//        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
//        Page<SetmealVO> page = setmealMapper.setmealPage(setmealPageQueryDTO);
//
//        return new PageResult(page.getTotal(),page.getResult());

        total = setmealMapper.getTotal();
        setmealPageQueryDTO.setPageSize(PAGESIZE);
        setmealPageQueryDTO.setPage((setmealPageQueryDTO.getPage() - 1) * PAGESIZE);

        List<SetmealPageVO> setmealPage = setmealMapper.setmealPage(setmealPageQueryDTO);

        return new PageResult(total,setmealPage);
    }

    /**
     * 删除套餐
     * @param ids
     * @return
     */
    @Override
    public void delete(List<Long> ids) {
        //当该套餐还在起售的时候不能删除
        for(Long id : ids) {
            //获取整个setmeal对象,再获取status
            Setmeal setmeal = setmealMapper.getByIds(id);
            //直接查询status状态
//            Long status = setmealMapper.getStatus(id);
            if(setmeal.getStatus().equals(StatusConstant.ENABLE)){
               throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }
        //如果是停售状态的套餐则删除
        setmealMapper.delete(ids);
    }

    /**
     * 新增套餐
     * @param setmealDTO
     * @return
     */
    @Transactional
    @Override
    public void saveWithDish(SetmealDTO setmealDTO) {
        //新增套餐菜品,允许不添加或添加则可以多个
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);

        setmealMapper.saveWithDish(setmeal);
        Long setmealId = setmeal.getId();

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if(setmealDishes != null && setmealDishes.size() > 0){
            setmealDishes.forEach(setmealDish ->{
                setmealDish.setSetmealId(setmealId);
            });
            setmealDishMapper.insertBatch(setmealDishes);
        }
    }

    /**
     * 根据id查询套餐信息,用于修改页面回显数据
     * @param id
     * @return
     */
    @Override
    public SetmealVO getById(Long id) {
        Setmeal setmeal = setmealMapper.getByIds(id);
        List<SetmealDish> setmealDishes = setmealDishMapper.getById(id);
        SetmealVO setmealVO = new SetmealVO();

        BeanUtils.copyProperties(setmeal,setmealVO);
        setmealVO.setSetmealDishes(setmealDishes);

        return setmealVO;
    }

    /**
     * 修改套餐
     * @param setmealDTO
     * @return
     */
    @Transactional
    @Override
    public void update(SetmealDTO setmealDTO) {

        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);

        //1、修改套餐表，执行update
        setmealMapper.update(setmeal);

        Long setmealId = setmealDTO.getId();

        //删除套餐和菜品的关联关系，操作setmeal_dish表，执行delete
        setmealDishMapper.deleteBySetmealId(setmealId);
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();

        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealId);
        });

        //重新插入套餐和菜品的关联关系,操作setmeal_dish表,执行insert
        setmealDishMapper.insertBatch(setmealDishes);

    }

    /**
     * 起售和停售
     * @param status
     * @param id
     * @return
     */
    @Override
    public void startAndStop(Integer status, Long id) {
        Setmeal setmeal = new Setmeal();
        setmeal.setId(id);
        setmeal.setStatus(status);
        setmealMapper.update(setmeal);
    }

    /**
     * C-端
     */
    /**
     * 根据category_id查询套餐
     * @param setmeal
     * @return
     */
    @Override
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    @Override
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
